package com.example.swagger2.controller;

import com.example.swagger2.dto.ReqDTO;
import com.example.swagger2.dto.ResponseDTO;
import com.example.swagger2.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.LinkedList;
import java.util.List;

@RestController//返回json格式
@RequestMapping("/user")
@Api(value = "用户操作API(V1)", tags = "用户操作接口")
@Slf4j
public class UserController {

    @ApiOperation("新增用户接口")
    @PostMapping("/add")
    public User addUser(@Validated @RequestBody User user) {
        log.info("addUser user={}", user);
        LocalDate birthDay = user.getBirthDay();
        //想要知道一下已经过了多少天
        LocalDate now = LocalDate.now();
        Period between = Period.between(birthDay, now);
        log.info("birthDay={} year={} days={}", birthDay, between.getYears(), between.getDays());
        long days = ChronoUnit.DAYS.between(birthDay, now);
        log.info("total days={}", days);
        return user;
    }

    @ApiOperation("通过id查找用户接口")
    @GetMapping("/find/{id}")
    public User findByid(@PathVariable("id") int id) {
        log.info("findByid id={}", id);
        User user = new User();
        user.setId(1);
        user.setAddress("宁波");
        user.setName("吴海峰");

        return user;
    }

    @ApiOperation("通过email和name查找用户接口")
    @GetMapping("/find/{email}/{name}")
    public User findByEmailAndName(@PathVariable("email") String email, @PathVariable("name") String name) {
        log.info("findByEmailAndName email={} name={}", email, name);

        User user = new User();
        user.setId(122);
        user.setAddress("宁波22");
        user.setName("吴海峰22");

        return user;
    }

    @ApiOperation("更新用户信息接口")
    @PutMapping("/update")
    public boolean update(@Validated @RequestBody User user) {
        log.info("update user={}", user);
        return true;
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("/delete/{id}")
    @ApiIgnore
    public boolean delete(@PathVariable("id") int id) {
        log.info("delete id={}", id);
        return true;
    }

    @ApiOperation("dbg测试")
    @PostMapping("/dbg")
    public ResponseDTO dbg(@RequestBody @Valid ReqDTO para) {
        log.info("para={}", para);
        ResponseDTO ret = new ResponseDTO().setNow(LocalDateTime.now()).setNow2(LocalDateTime.now());
        log.info("ret={}", ret);

        return ret;
    }
}
